<?php

namespace app\admin\controller;

use app\admin\logic\AdminLogic;
use app\common\exception\ApiErrDesc;
use app\common\exception\ApiException;
use think\App;
use think\facade\Validate;
use think\validate\ValidateRule as Rule;


/**
 * 管理员控制器
 * Class AdminController
 * @package app\admin\controller
 * @author ^2_3^王尔贝
 */
class AdminController extends EndController
{
    /**
     * 不用检测操作
     * @var array
     */
    protected $notCheck = ['login', 'register'];

    private $adminLogic;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->adminLogic = new AdminLogic();
    }

    /**
     * 个人设置
     * @return \think\response\Json
     * @author ^2_3^王尔贝
     */
    public function basicSetting()
    {
        $params = $this->request->post();

        $validate = Validate::rule([
            'email' => Rule::isEmail(),
        ]);
        if (!$validate->check($params)) {
            return retJson($validate->getError(), ApiErrDesc::ERR_VALIDATE[0]);
        }

        $params['id'] = $this->admin['info']['id'];
        $flag = $this->adminLogic->basicSetting($params);
        if ($flag === false) {
            return retJson('basic setting error', ApiErrDesc::ERR_UNKNOWN[0]);
        } else {
            $basicInfo = $flag[0];
            $basicInfo['avatar'] = "{$basicInfo['avatar']}";
            return retJson('', 0, $basicInfo);
        }
    }

    /**
     * 处理头像
     * @return \think\response\Json
     * @author ^2_3^王尔贝
     */
    public function handleAvatar()
    {
        $avatar = $this->request->file('file');
        if ($avatar) {
            $admin = $this->admin['info'];
            $adminInfo = [
                'id' => $admin['id'],
                'username' => $admin['username'],
                'avatar' => $admin['avatar'],
            ];
            $flag = $this->adminLogic->handleAvatar($avatar, $adminInfo);

            if ($flag !== false) {
                return retJson('', 0, ['url' => $flag]);
            }
        }
        return retJson('avatar is invalid', ApiErrDesc::ERR_VALIDATE);
    }

    /**
     * 管理人员信息
     * @return \think\response\Json
     * @author ^2_3^王尔贝
     */
    public function info()
    {
        $params = $this->request->get();
        $params['info'] = $this->admin['info'];
        $params['roleIds'] = $this->admin['roleIds'];
        $info = $this->adminLogic->info($params);

        return retJson('', 0, $info);
    }

    /**
     * 分页列表
     * @return \think\response\Json
     * @author ^2_3^王尔贝
     */
    public function listPage()
    {
        $params = $this->request->post();
        $listPage = $this->adminLogic->listPage($params);
        if (!empty($listPage)) {
            $listPage = $this->transformPage($listPage->toArray());
        }
        return retJson('', 0, $listPage);
    }

    /**
     * 登出
     * @return \think\response\Json
     * @author ^2_3^王尔贝
     */
    public function logout()
    {
        $params = $this->request->post();
        $params['admin_id'] = $this->admin['info']['id'];
        return retJson('', 0, $params);
    }

    /**
     * token检测
     * @return \think\response\Json
     * @author ^2_3^王尔贝
     */
    public function checkToken()
    {
        $params = $this->request->post();

        $validate = Validate::rule([
            'developer' => Rule::isRequire(),
        ]);
        if (!$validate->check($params)) {
            return retJson($validate->getError(), ApiErrDesc::ERR_VALIDATE[0]);
        }

        $data = $params;
        return retJson('', 0, $data);
    }

    /**
     * 后台
     * @return \think\response\Json
     * @author ^2_3^王尔贝
     */
    public function login()
    {
        $params = $this->request->post();

        $validate = Validate::rule([
            'username' => Rule::isRequire(),
            'password' => Rule::isRequire(),
        ]);
        if (!$validate->check($params)) {
            return retJson($validate->getError(), ApiErrDesc::ERR_VALIDATE[0]);
        }

        $flag = $this->adminLogic->login($params);
        if ($flag !== false) {
            $data = [
                'id' => $flag['id'],
                'username' => $flag['username'],
                'token' => $flag['token'],
            ];
            return retJson('', 0, $data);

        } else {
            return retJson('账号登录错误', ApiErrDesc::ERR_UNKNOWN[0]);
        }
    }

    /**
     * 修改密码
     * @return \think\response\Json
     * @author ^2_3^王尔贝
     */
    public function changePass()
    {
        $params = $this->request->post();
        $validate = Validate::rule([
            'ori_pass' => Rule::isRequire(),
            'pass' => Rule::isRequire(),
            'check_pass' => Rule::isRequire(),
        ]);
        if (!$validate->check($params)) {
            return retJson($validate->getError(), ApiErrDesc::ERR_VALIDATE[0]);
        }
        if ($params['pass'] != $params['check_pass']) {
            return retJson('两次密码不匹配~', ApiErrDesc::ERR_VALIDATE[0]);
        }

        $admin = $this->admin['info'];
        $params['id'] = $admin['id'];
        $params['salt'] = $admin['salt'];
        $params['password'] = $admin['password'];
        $flag = $this->adminLogic->changePass($params);
        if ($flag) {
            return retJson('', 0, $flag);
        } else {
          return retJson('error~', ApiErrDesc::ERR_UNKNOWN[0]);
        }
    }

    /**
     * 更新
     * @return \think\response\Json
     * @author ^2_3^王尔贝
     */
    public function edit()
    {
        $params = $this->request->post();
        //// 参数验证
        $validate = Validate::rule([
            'id' => Rule::isRequire(),
            'username'  => Rule::isRequire(),
        ]);
        if( !$validate->check( $params ) ) {
            return retJson($validate->getError(), ApiErrDesc::ERR_VALIDATE[0]);
        }

        $flag = $this->adminLogic->save( $params );
        if( $flag !== false ) {
            return retJson('', 0);

        }else {
            return retJson('更新失败，稍后再试', ApiErrDesc::ERR_UNKNOWN[0]);
        }
    }

    /**
     * 注册
     * @return \think\response\Json
     * @author ^2_3^王尔贝
     */
    public function register()
    {
        $params = $this->request->post();
        //// 参数验证
        $validate = Validate::rule([
            'username'  => Rule::isRequire(),
            'password' => Rule::isRequire(),
        ]);
        if( !$validate->check( $params ) ) {
            return retJson($validate->getError(), ApiErrDesc::ERR_VALIDATE[0]);
        }

        $flag = $this->adminLogic->register( $params );
        if( $flag !== false ) {
            return retJson('', 0);

        }else {
            return retJson('注册失败，稍后再试', ApiErrDesc::ERR_UNKNOWN[0]);
        }
    }
}